package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.WmsAssistDelivery;
import com.jy.wms.service.WmsAssistDeliveryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @ClassName: WmsAssistDeliveryController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Thu Mar 15 11:02:39 CST 2018
 */
@RestController
@RequestMapping("/wmsassistdelivery")
@Api(value = "/wmsassistdelivery", description = "协助发货相关操作")
public class  WmsAssistDeliveryController extends BaseController<WmsAssistDeliveryService,WmsAssistDelivery> {


    @PostMapping(value = "/upload")
    @ApiOperation(value = "协助发货 上传EXCEL文件导入", response = JSONObject.class, notes = "上传EXCEL文件导入")
    public @ResponseBody
    JSONObject upload(HttpSession session, @RequestBody String filepath){
        try {

            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            baseService.upload(filepath, String.valueOf(userinfo.getUser().getOrgId().get(0)));
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"导入EXCEL成功","", "");
        } catch (SucessException e){
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"导入EXCEL成功",e.getMessage(), "");
        } catch (Exception e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"导入EXCEL失败",e.getMessage(), "");
        }
    }

    @PostMapping(value="/list")
    @ApiOperation(value = "协助发货 分页查询",response = JSONObject.class, notes = "分页查询")
    public@ResponseBody JSONObject getWmsOwnerOrdertype(@RequestBody WmsAssistDelivery entity, HttpSession session) {
        Page<WmsAssistDelivery> list = null;
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            list= baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @PostMapping(value="/edit")
    @ApiOperation(value = "协助发货  编辑信息",response = JSONObject.class, notes = "编辑信息")
    public@ResponseBody JSONObject edit(@RequestBody WmsAssistDelivery entity, HttpSession session) {
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"编辑信息成功","", "");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"编辑信息成功","", "");
        }catch (ServiceException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"编辑信息失败",e.getMessage(), "");
        }catch (Exception e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"编辑信息失败",e.getMessage(), "");
        }
    }

    @PostMapping(value="/del")
    @ApiOperation(value = "协助发货  删除",response = JSONObject.class, notes = "删除")
    public@ResponseBody JSONObject del(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        try {
            baseService.baseDeleteBathPrimaryKeys(setAndModifyModel.getIds());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除成功","", "");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除成功","", "");
        }catch (Exception e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除失败",e.getMessage(), "");
        }
    }

    @PostMapping(value="/xfpda")
    @ApiOperation(value = "协助发货  下发PDA",response = JSONObject.class, notes = "下发PDA")
    public@ResponseBody JSONObject xfpda(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        try {
            baseService.xfpda(setAndModifyModel,session);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"下发PDA成功","", "");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"下发PDA成功","", "");
        }catch (ServiceException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"下发PDA失败",e.getMessage(), "");
        }catch (Exception e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"下发PDA失败",e.getMessage(), "");
        }
    }

    @PostMapping(value="/pcfy")
    @ApiOperation(value = "协助发货  PC端发运",response = JSONObject.class, notes = "PC端发运")
    public@ResponseBody JSONObject pcfy(@RequestBody SetAndModifyModel setAndModifyModel, HttpSession session) {
        try {
            baseService.pcfy(setAndModifyModel.getIds());
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"PC端发运成功","", "");
        }catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"PC端发运成功","", "");
        }catch (ServiceException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"PC端发运失败",e.getMessage(), "");
        }catch (Exception e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"PC端发运失败",e.getMessage(), "");
        }
    }

    @GetMapping(value="/findByDdh/{ddh}")
    @ApiOperation(value = "协助发货  根据订单号查询协助发货信息",response = JSONObject.class, notes = "根据订单号查询协助发货信息")
    public@ResponseBody JSONObject findByDdh(@PathVariable("ddh") String ddh) {
        JSONObject json = getJSONObject();
        try {
            json = SetStatus.setSearchType(JSONArray.fromObject(baseService.findByDdh(ddh)).toString(),1);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @GetMapping(value="/print/{id}/{sjzl}/{cyslb}")
    @ApiOperation(value = "协助发货  打印快递面单",response = JSONObject.class, notes = "打印快递面单")
    public@ResponseBody JSONObject print(@PathVariable("id") String id, @PathVariable("sjzl") String sjzl, @PathVariable("cyslb") Integer cyslb) {
        try {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"打印快递面单成功","",baseService.print(id,sjzl,cyslb));
        }catch (ServiceException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"打印快递面单失败",e.getMessage(),"");
        }catch (Exception e) {
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"打印快递面单失败",e.getMessage(),"");
        }
    }
}
